

package modelos;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Implementacion patron abstract Factory
public class MIngrediente implements modeloAbstractFactory {

    private String descripcion,codigo,status;
    private float stock;
    private MConexion conexion;

    public MIngrediente() {
        super();
        conexion = new MConexion();
    }

    public int generar(){
        return conexion.generarCodigo("Ingrediente");
    }
  //Implementa patron abstract factory
    public void registrar(){
    
        conexion.conectar();
        boolean sw = false;
        try {
        	String SQL= "INSERT INTO Ingrediente (codigo, descripcion, stock, status) VALUES('"+getCodigo()+"','"+getDescripcion()+"',"+getStock()+",'A')";
            conexion.getConexion().createStatement().execute(SQL);
            sw= true;
        } catch (SQLException ex) {
             sw= false;
        }
        
        
        conexion.desconectar();
      //  return sw;
    }

  public boolean actualizar(){
     conexion.conectar();
     boolean sw = false;
     try {
    	 String SQL= "UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo='"+getCodigo()+"'  AND status = 'A'";
         conexion.getConexion().createStatement().execute(SQL);
         sw= true;
     } catch (SQLException ex) {
            Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
     }
     conexion.desconectar();
     return sw;
 }

  public boolean  consultar(){
    conexion.conectar();
    boolean sw = false;
    try {
    	String SQL= "SELECT * FROM Ingrediente WHERE codigo='"+getCodigo()+"' AND status='A'";
        ResultSet rs= conexion.getConexion().createStatement().executeQuery(SQL);
        if (rs.next()){
            sw = true;
            setDescripcion(rs.getString("descripcion"));
            setCodigo(rs.getString("codigo"));
            setStatus(rs.getString("status"));
            setStock(rs.getFloat("stock"));
         }
    } catch (SQLException ex) {
        Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    return sw;
  }

  public ArrayList<MIngrediente> listarIngredientes(){
    ArrayList<MIngrediente> v = new ArrayList<MIngrediente>();
    conexion.conectar();
    try {
    	String SQL= "SELECT * FROM Ingrediente WHERE status='A'";
        ResultSet rs= conexion.getConexion().createStatement().executeQuery(SQL);
        while (rs.next()){
            MIngrediente  i = new MIngrediente();
            i.setDescripcion(rs.getString("descripcion"));
            i.setCodigo(rs.getString("codigo"));
            i.setStatus(rs.getString("status"));
            i.setStock(rs.getFloat("stock"));
            v.add(i);
         }
    } catch (SQLException ex) {
        Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    conexion.desconectar();
    return v;
  }

  public ArrayList<String> listarProdcutos(){
    conexion.conectar();
      ArrayList<String> v = new ArrayList<String>();
    try {
    	String SQL = "SELECT Producto.descripcion FROM  Producto,IngredientexProducto,Ingrediente WHERE Ingrediente.status='A'  AND Producto.status='A' AND Ingrediente.codigo=IngredientexProducto.codigo_ing AND Ingrediente.codigo='"+getCodigo()+"'";
        ResultSet rs = conexion.getConexion().createStatement().executeQuery(SQL);
        while (rs.next())
            v.add(rs.getString(1));
    } catch (SQLException ex) {
        Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
    }
    conexion.desconectar();
    return v;
  }
  
  public boolean disponible(float porcion){
      consultar();
      if((getStock()-porcion)>=0)
          return true;
      else
          return false;
  }

  public void restarStock(){
       conexion.conectar();
       try {
    	   String SQL="UPDATE Ingrediente SET stock ="+getStock()+" WHERE codigo='"+getCodigo()+"'  AND status = 'A'";
           conexion.getConexion().createStatement().execute(SQL);
       } catch (SQLException ex) {
           Logger.getLogger(MIngrediente.class.getName()).log(Level.SEVERE, null, ex);
       }
       conexion.desconectar();
  }
  
  public String getDescripcion() {
        return descripcion;
  }

  public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
  }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

    public float getStock() {
        return stock;
    }

    public void setStock(float stock) {
        this.stock = stock;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }



}



